<template>
  <a-drawer :title="title" :width="width" placement="right" :closable="false" @close="handleCancel" :visible="visible">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="员工编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                :disabled="disableSubmit"
                v-decorator="['partyId', validatorRules.partyId]"
                dict="Person,name,partyId,typeId = EMPLOYEE"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select
                :disabled="disableSubmit"
                v-decorator="['departmentId', validatorRules.departmentId]"
                pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                placeholder="请选择部门"
                back="name"
                @change="handleCategoryChange"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
               <a-input v-decorator="['jobId',validatorRules.jobId]" :disabled="disableSubmit" placeholder="请输入职位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="转正日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择转正日期"
                :disabled="disableSubmit"
                v-decorator="['becomeDate', validatorRules.becomeDate]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="转正原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['description', validatorRules.description]"
                rows="4"
                placeholder="请输入转正原因"
              />
              <!-- <a-input v-decorator="['description',validatorRules.description]" :disabled="disableSubmit" placeholder="请输入转正原因"></a-input> -->
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
              <!-- <a-input :disabled="disableSubmit" v-decorator="['remark']" placeholder="请输入备注"></a-input> -->
            </a-form-item>
          </a-col>
          <a-col :span="24"
            ><a-button @click="handleCancel">关闭</a-button>
            <a-button v-if="!disableSubmit" type="primary" @click="handleOk" :loading="buttonok">确定</a-button></a-col
          >
        </a-row>
      </a-form>
    </a-spin>
  </a-drawer>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JCategorySelect from '@/components/jeecg/JCategorySelect'

export default {
  name: 'HrBecomeFullMemberModal',
  components: {
    JDate,
    JDictSelectTag,
    JSearchSelectTag,
    JCategorySelect,
  },
  data() {
    return {
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 600,
      visible: false,
      buttonok: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        partyId: {
          rules: [{ required: true, message: '请输入员工编号!' }],
        },
        departmentId: {
          rules: [{ required: true, message: '请输入部门!' }],
        },
        jobId: {
          rules: [{ required: true, message: '请输入职位!' }],
        },
        becomeDate: {
          rules: [{ required: true, message: '请选择转正日期!' }],
        },
        description: {
          rules: [{ required: true, message: '转正原因!' }],
        },
        isBecome: {
          rules: [{ required: true, message: '请输入是否转正!' }],
        },
      },
      url: {
        add: '/online/api/createHrBecomeFullMember',
        edit: '/online/api/updateHrBecomeFullMember',
        get: '/online/api/getHrBecomeFullMember',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        httpAction(this.url.get, { id: record.id }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'partyId',
                  'departmentId',
                  'jobId',
                  'becomeDate',
                  'description',
                  'createDate',
                  'createBy',
                  'statusId',
                  'isBecome',
                  'remark'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          that.buttonok = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
            this.model.statusId = '0'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
              setTimeout(() => { that.buttonok = false; }, 500);
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'partyId',
          'departmentId',
          'jobId',
          'becomeDate',
          'description',
          'createDate',
          'createBy',
          'statusId',
          'isBecome',
          'remark'
        )
      )
    },
    handleCategoryChange(value, backObj) {
      this.form.setFieldsValue(backObj)
    },
  },
}
</script>
<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 30px;
  margin-bottom: 30px;
  float: right;
}
</style>
